<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 後臺 管理員權限設置
 *@param datetime 2012-01-06
 *@param author solgh
 ********************/
class Users_group extends Admins_Controller {
  
  private $data=array();
  private $admin_id;
  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='systems';
    $this->load->model('admins_group_mdl');
    $this->data['format_time']=$this->settings['default_time'];
	$lan_get = 	$this->input->get('lan','');
    $lan_get = empty($lan_get) ? get_cookie('language') : $lan_get;
      if(empty($lan_get) || !in_array($lan_get,array('zh-tw','english'))){
          $lan_get = 'english';
      }
    $this->lang->load('page', $lan_get);
    set_cookie('language',$lan_get,time()+24*3600*30);
    $this->data['getlang'] = $lan_get;
    $this->data['lang'] =$this->lang->language;
    $this->get_permissions();//獲取權限
  }

  public function index(){
    $this->get_list();
    $this->load->view('admins/users_group_list',$this->data);
  }
  
  //獲取列表
  private function get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    $limit = $this->settings['default_admin_limit'];
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admins/users_group');
    }
    $url_query=$_SERVER['QUERY_STRING'];
    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }
    $this->data['name']=$this->input->get('name');
    $this->data['ename']=$this->input->get('ename');
    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'add_time';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'asc';
    $data=array('limits'=>$limit,'offset'=>$offset);
    
    $data=!empty($this->data['name'])?array_merge($data,array('name'=>trim($this->data['name']))):$data;
    $data=!empty($this->data['ename'])?array_merge($data,array('ename'=>trim($this->data['ename']))):$data;
    $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;
    $data=!empty($this->data['sort'])?array_merge($data,array('sort'=>strtoupper($this->data['sort']))):$data;
    
    $this->data['lists_count']=$this->admins_group_mdl->totle_admins_group($data);
    
    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(5);
      $this->dpagination->target(site_url('admins/users_group').'?'.$url_query);
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
      $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->admins_group_mdl->get_admins_group($data);
  }
  
  //添加管理員.
  public function add()
  {
    $this->load->view('admins/users_group_form',$this->data);
  }
  //修改信息
  public function edit($edit_id=0)
  {
    if(!empty($edit_id)){
      $result=$this->admins_group_mdl->get_admins_group_by_id($edit_id);
      $this->data['edit_id']=$edit_id;
      $this->data['name']=$result['name'];
	  $this->data['ename']=$result['ename'];
      $this->data['permission']=unserialize($result['permission']);
      $this->load->view('admins/users_group_form',$this->data);
    }else{
      show_error('對不起,參數出錯!');
      exit;
    }
  }
  //執行添加.
  public function modify()
  {
     $edit_id=$this->input->post('edit_id');
     $this->admin_id=$edit_id;
     $this->_load_validation_rules($edit_id);
     if ($this->form_validation->run() == FALSE){
        $this->data['edit_id']=$edit_id;
        $this->data['error']=true;
        $this->load->view('admins/users_group_form',$this->data);
     }else{
       $permission=$this->input->post('permission');
        $data=array(
            'name'=>$this->input->post('name'),
			'ename'=>$this->input->post('ename'),
            'permission'=>!empty($permission)?serialize($permission):'',
        );
        if(!empty($edit_id) && $edit_id>0){
          $operator_title='修改用戶組->'.$data['name'];
          $action='修改';
          $result=$this->admins_group_mdl->edit_admins_group($data,$edit_id);
        }else{
          $operator_title='新增用戶組->'.$data['name'];
          $action='新增';
          $data=array_merge($data,array('add_time'=>time()));
          $result=$this->admins_group_mdl->add_admins_group($data);
        }
        $this->operator_log($operator_title,$action,$result);
        $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
        $notify = $result > 0?'success':'error';
        $this->nsession->set_flashdata($notify, $msg);
        redirect("admins/users_group");
      }
  }
  //驗證數據格式
  private function _load_validation_rules($edit_id=0){
      $this->form_validation->set_rules('name','用戶組','trim|required|is_unique[clickrcms_admin_group.name.id.'.$this->admin_id.']|max_length[50]');
	  $this->form_validation->set_rules('ename','用戶組','trim|required|is_unique[clickrcms_admin_group.ename.id.'.$this->admin_id.']|max_length[50]');
      $this->form_validation->set_rules('permission[]','用戶組權限','trim|required');
  }
  //刪除管理員
  public function delete($delete_id=''){
   if(!empty($delete_id)){
      $result=$this->admins_group_mdl->delete_admins_group($delete_id);
      $this->operator_log('刪除用戶組->ID:'.$delete_id,'刪除',$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect("admins/users_group");
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }
  
  //權限
  private function get_permissions(){
    $files = glob(FCPATH.APPPATH.'controllers/admins/*.php');
    $this->data['permissions'] = array();
    $ignore = array(
      'login',
      'upload'
    );
    foreach ($files as $file) {
      $permission = basename($file, '.php');
      if (!in_array($permission, $ignore)) {
        $this->data['permissions'][] = $permission;
      }
    }
    
    $this->data['perstring']=array(
      //基本
      'settings'=>$this->data['lang']['settings_lable_1'],
      'users'=>$this->data['lang']['admin_lable_2'],
      'users_group'=>$this->data['lang']['admin_lable_5'],
      'advertisings'=>$this->data['lang']['content_lable_8'],
      'backup'=>$this->data['lang']['admin_lable_37'],
      'blog'=>$this->data['lang']['admin_lable_38'],
      'blog_category'=>$this->data['lang']['admin_lable_39'],
      'feedbacks'=>$this->data['lang']['admin_lable_40'],
      'index'=>$this->data['lang']['global_lable_11'],
      'languages'=>$this->data['lang']['admin_lable_41'],
      'operator'=>$this->data['lang']['admin_lable_12'],
      'pages'=>$this->data['lang']['admin_lable_42'],
      'pays'=>$this->data['lang']['admin_lable_35'],
      //常變
      'categorys'=>$this->data['lang']['content_lable_3'],
      'products'=>$this->data['lang']['content_lable_4'],
      'productunits'=>$this->data['lang']['content_lable_6'],
      // 'projecttype'=>'過往業績類別',
      // 'news'=>'最新消息',
      // 'teams'=>'團隊介紹',
      // 'informations'=>'資料索取',
      // 'jobs'=>'招聘職位',
      // 'recruitments'=>'求職申請',
    );
  }
}